<?php

namespace App\Admin\Metrics\Charts;

use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\ApexCharts\Chart;
use Carbon\Carbon;
use App\Models\ViewCount;

class VisitLIne extends Chart
{
    public function __construct($containerSelector = null, $options = [])
    {
        parent::__construct($containerSelector, $options);

        $this->setUpOptions();
    }

    /**
     * 初始化图表配置
     */
    protected function setUpOptions()
    {
        $color = Admin::color();

        $colors = [$color->primary(), '#FF6666','#66CC00'];
        $title = '近 7 日走势图';
        $this->options([
            'colors' => $colors,
            'chart' => [
                'type' => 'line',
                'height' => 430,
                'dropShadow'=>[
                    'enabled'=> true,
                    'color'=>'#000',
                    'top'=> 18,
                    'left'=> 7,
                    'blur'=> 10,
                    'opacity'=> 0.2
                ],
                'toolbar'=>[
                    'show'=>false
                ]
            ],
            'dataLabels'=>[
                'enabled'=>true,
            ],
            'stroke'=>[
                'curve'=>'smooth'
            ],
            'title'=>[
                'text'=>$title,
                'align'=> 'left'
            ],
            'xaxis' => [
                'categories' => [],
            ],
        ]);
    }

    /**
     * 处理图表数据
     */
    protected function buildData()
    {
        $today = Carbon::today();
        $categories = [substr($today,0,10),substr($today->addDays(-1),0,10),substr($today->addDays(-1),0,10),substr($today->addDays(-1),0,10),substr($today->addDays(-1),0,10),substr($today->addDays(-1),0,10),substr($today->addDays(-1),0,10)];
        sort($categories);

        //入口浏览量
        $index_pv_01 = ViewCount::whereDate('created_at','>=',Carbon::today())->where('category',1)->count();
        $index_pv_02 = ViewCount::whereDate('created_at', '>=', Carbon::today()->addDays(-1))->whereDate('created_at', '<', Carbon::today())->where('category',1)->count();
        $index_pv_03 = ViewCount::whereDate('created_at', '>=', Carbon::today()->addDays(-2))->whereDate('created_at', '<', Carbon::today()->addDays(-1))->where('category',1)->count();
        $index_pv_04 = ViewCount::whereDate('created_at', '>=', Carbon::today()->addDays(-3))->whereDate('created_at', '<', Carbon::today()->addDays(-2))->where('category',1)->count();
        $index_pv_05 = ViewCount::whereDate('created_at', '>=', Carbon::today()->addDays(-4))->whereDate('created_at', '<', Carbon::today()->addDays(-3))->where('category',1)->count();
        $index_pv_06 = ViewCount::whereDate('created_at', '>=', Carbon::today()->addDays(-5))->whereDate('created_at', '<', Carbon::today()->addDays(-4))->where('category',1)->count();
        $index_pv_07 = ViewCount::whereDate('created_at', '>=', Carbon::today()->addDays(-6))->whereDate('created_at', '<', Carbon::today()->addDays(-5))->where('category',1)->count();
        
        //落地
        $born_openid_01 = ViewCount::whereDate('created_at','>=',Carbon::today())->where('category',2)->pluck('openid');
        $born_openid_02 = ViewCount::whereDate('created_at', '>=', Carbon::today()->addDays(-1))->whereDate('created_at', '<', Carbon::today())->where('category',2)->pluck('openid');
        $born_openid_03 = ViewCount::whereDate('created_at', '>=', Carbon::today()->addDays(-2))->whereDate('created_at', '<', Carbon::today()->addDays(-1))->where('category',2)->pluck('openid');
        $born_openid_04 = ViewCount::whereDate('created_at', '>=', Carbon::today()->addDays(-3))->whereDate('created_at', '<', Carbon::today()->addDays(-2))->where('category',2)->pluck('openid');
        $born_openid_05 = ViewCount::whereDate('created_at', '>=', Carbon::today()->addDays(-4))->whereDate('created_at', '<', Carbon::today()->addDays(-3))->where('category',2)->pluck('openid');
        $born_openid_06 = ViewCount::whereDate('created_at', '>=', Carbon::today()->addDays(-5))->whereDate('created_at', '<', Carbon::today()->addDays(-4))->where('category',2)->pluck('openid');
        $born_openid_07 = ViewCount::whereDate('created_at', '>=', Carbon::today()->addDays(-6))->whereDate('created_at', '<', Carbon::today()->addDays(-5))->where('category',2)->pluck('openid');

        //落地pv
        $born_pv_01 = count($born_openid_01);
        $born_pv_02 = count($born_openid_02);
        $born_pv_03 = count($born_openid_03);
        $born_pv_04 = count($born_openid_04);
        $born_pv_05 = count($born_openid_05);
        $born_pv_06 = count($born_openid_06);
        $born_pv_07 = count($born_openid_07);

        //落地uv
        $born_uv_01 = count(array_unique($born_openid_01->toArray()));
        $born_uv_02 = count(array_unique($born_openid_02->toArray()));
        $born_uv_03 = count(array_unique($born_openid_03->toArray()));
        $born_uv_04 = count(array_unique($born_openid_04->toArray()));
        $born_uv_05 = count(array_unique($born_openid_05->toArray()));
        $born_uv_06 = count(array_unique($born_openid_06->toArray()));
        $born_uv_07 = count(array_unique($born_openid_07->toArray()));

        // 执行你的数据查询逻辑
        $data = [
            [
                'name'=> "入口浏览量 PV",
                'data' => [$index_pv_07,$index_pv_06, $index_pv_05, $index_pv_04, $index_pv_03, $index_pv_02, $index_pv_01]
            ],
            [
                'name'=> "落地浏览量 PV",
                'data' => [$born_pv_07,$born_pv_06,$born_pv_05,$born_pv_04,$born_pv_03,$born_pv_02,$born_pv_01]
            ],
            [
                'name'=> "落地用户数 UV",
                'data' =>  [$born_uv_07,$born_uv_06,$born_uv_05,$born_uv_04,$born_uv_03,$born_uv_02,$born_uv_01]
            ]
        ];
        $this->withData($data);
        $this->withCategories($categories);
    }

    /**
     * 设置图表数据
     *
     * @param array $data
     *
     * @return $this
     */
    public function withData(array $data)
    {
        return $this->option('series', $data);
    }

    /**
     * 设置图表类别.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withCategories(array $data)
    {
        return $this->option('xaxis.categories', $data);
    }

    /**
     * 渲染图表
     *
     * @return string
     */
    public function render()
    {
        $this->buildData();

        return parent::render();
    }
}